<?php


namespace app\admin\controller;

use app\admin\model\KeywordModel;

/**
 * Class Keyword
 * @package app\admin\controller
 * 敏感词配置
 */
class Keyword extends Main{
    /**
     * 跳转到分页查询页面
     */

    public function gotoKeyword(){
        return $this->fetch("keywords");
    }

    public function goAddKeyword(){
        return $this->fetch("add");

    }
    /**
     * 分页查询敏感词
     */
    public function getKeywords(){
        $page = isset($_GET['page'])?$_GET['page']:1;
        $size = isset($_GET['size'])?$_GET['size']:10;
        $keywordModel = new KeywordModel();
        $keywords = $keywordModel->limit(($page-1) * $size, $size)->order('id', 'desc')->select();
        $count = $keywordModel->count('id');
        $this->assign('keywords', $keywords);
        $result = [];
        $result["code"] = 0;
        $result["msg"] = "";
        $result["count"] = count($count);
        $result["data"] = $keywords;
        return json($result);
    }

    /**
     * 添加敏感词
     */
    public function addKeyword(){
        $keyword = isset($_POST['keyword'])?$_POST['keyword']:"";
        $result = [];
        if($keyword!=""){
            $keywordModel = new KeywordModel();
            $selectResult = $keywordModel->where("keyword", "=", $keyword)->select();
            if(empty($selectResult)){
                $keywordModel->data([
                    'keyword'=>$keyword,
                    'add_user_name'=>"罗龙",
                    'add_user_id'=>"11",
                ])->save();
                $result["code"] = 0;
                $result["msg"] = "添加成功";
            }else{
                $result["code"] = -1;
                $result["msg"] = "该关键字已经存在";
            }

        }else{
            $result["code"] = -1;
            $result["msg"] = "添加失败";
        }

        $result["data"] = "";
        return json($result);
    }

    /**
     * 删除敏感词
     */
    public function delKeyword(){
        $id = isset($_GET['id'])?$_GET['id']:"";
        $keywordModel = new KeywordModel();
        $result = [];
        if($id!=""){
            $keywordModel->where("id","=",$id)->delete();
            $result["code"] = 0;
            $result["msg"] = "删除成功";
        }else{
            $result["code"] = -1;
            $result["msg"] = "删除失败";
        }
        $result["data"] = "";
        return json($result);
    }

    /**
     * 更新敏感词
     */
    public function updateKeyword(){
        $id = isset($_GET['id'])?$_GET['id']:"";
        $keyword = isset($_GET['keyword'])?$_GET['keyword']:"";
        $result = [];
        if($id!=""&&$keyword!=""){
             KeywordModel::update(["keyword"=>$keyword],["id"=>$id],["keyword"]);
            $result["code"] = 200;
            $result["msg"] = "更新成功";
            $result["data"] = "更新成功";

        }else{
            $result["code"] = 0;
            $result["msg"] = "更新失败";
            $result["data"] = "更新失败";
        }
        return json($result);
    }
}